<template>
  <el-dialog :visible.sync="visible" v-dialogDrag :title="title" :close-on-click-modal="false" class="tl-dialog" width="1200px" @open="open" @close="close">
    <el-form class="tl-form" ref="form" :model="model" :rules="rules" label-width="150px" :disabled="disableEditSubmit">
			<el-row>
				<el-col :span="12">
					<el-form-item label="部门统称" prop="deptcommoncode" :rules="rules.validateRequire" label-width="180px">
						<el-select v-model="model.deptcommoncode" filterable clearable>
							<el-option v-for="item in codeModel.deptcommoncodeList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="岗位" prop="postcode" :rules="rules.validateRequire">
						<el-select v-model="model.postcode" filterable clearable>
							<el-option v-for="item in codeModel.postcodeList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
        <el-col :span="12">
          <el-form-item label="岗位重量" prop="postweight" :rules="rules.validateRequire" label-width="180px">
            <el-input-number v-model="model.postweight" style="width:100%;"></el-input-number>
          </el-form-item>
        </el-col>
				<el-col :span="12">
					<el-form-item label="岗位员工工号" prop="usercode"  :rules="rules.validateRequire">
            <tl-ref-user v-model="model.usercode" :initDisplayData.sync="model.username" dataType="code" :singleSelect="false"></tl-ref-user>
					</el-form-item>
				</el-col>
			</el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="可见创建部门授权类型" prop="visibledeptcommoncodetype"  label-width="180px">
						<el-select v-model="model.visibledeptcommoncodetype" filterable clearable>
							<el-option v-for="item in codeModel.visibledeptcommoncodetypeList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
      <el-row v-if="sectionValidate('1')">
        <el-col :span="24">
          <!-- <el-form-item label="可见创建部门授权范围" prop="visibledeptcommoncoderange" label-width="180px"> -->
          <el-form-item label="可见创建部门授权范围" prop="visibledeptcommoncoderange" label-width="180px" :rules="sectionValidate('1')?rules.validateRequire:[]">
            <el-select v-model="arrayvisibledeptcommon_code_range" multiple filterable clearable>
              <el-option v-for="item in codeModel.visibledeptcommoncoderangeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="可见产品线授权类型" prop="visibleproductlinetype" label-width="180px">
            <el-select v-model="model.visibleproductlinetype" filterable clearable>
              <el-option v-for="item in codeModel.visibleproductlinetypeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row v-if="sectionValidate('2')">
        <el-col :span="24">
          <!-- <el-form-item label="可见产品线授权范围" prop="visibleproductlinerange" label-width="180px"> -->
            <el-form-item label="可见产品线授权范围" prop="visibleproductlinerange" label-width="180px" :rules="sectionValidate('2')?rules.validateRequire:[]">
            <el-select v-model="arrayvisible_productline_range" multiple filterable clearable>
              <el-option v-for="item in codeModel.visibleproductlinerangeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="可见创建部门免授权类型" prop="novisiblecommoncodetype" label-width="180px">
						<el-select v-model="model.novisiblecommoncodetype" filterable clearable>
							<el-option v-for="item in codeModel.novisiblecommoncodetypeList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
      <el-row v-if="sectionValidate('3')">
        <el-col :span="24">
          <!-- <el-form-item label="可见创建部门免授权范围" prop="novisiblecommoncoderange" label-width="180px"> -->
          <el-form-item label="可见创建部门免授权范围" prop="novisiblecommoncoderange" label-width="180px" :rules="sectionValidate('3')?rules.validateRequire:[]">
            <el-select v-model="noarrayvisible_common_code_range" multiple filterable clearable>
              <el-option v-for="item in codeModel.novisiblecommoncoderangeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="可见产品线免授权类型" prop="novisibleproductlinetype" label-width="180px">
						<el-select v-model="model.novisibleproductlinetype" filterable clearable>
							<el-option v-for="item in codeModel.novisibleproductlinetypeList" :key="item.code" :label="item.name" :value="item.code" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
      <el-row v-if="sectionValidate('4')">
        <el-col :span="24">
          <!-- <el-form-item label="可见产品线免授权范围" prop="novisibleproductlinerange" label-width="180px"> -->
          <el-form-item label="可见产品线免授权范围" prop="novisibleproductlinerange" label-width="180px" :rules="sectionValidate('4')?rules.validateRequire:[]">
            <el-select v-model="noarrayvisible_productline_range" multiple filterable clearable>
              <el-option v-for="item in codeModel.novisibleproductlinerangeList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
			<el-row>
				<el-col :span="12">
					<el-form-item label="是否必选" prop="whethermust" label-width="180px" :rules="rules.validateRequire">
						<el-select v-model="model.whethermust" filterable clearable>
							<el-option v-for="item in codeModel.whethermustList" :key="item.code" :label="item.name" :value="parseInt(item.code)" />
						</el-select>
					</el-form-item>
				</el-col>
        <el-col :span="12">
          <el-form-item label="是否默认勾选" prop="whetherdefault" label-width="200px" :rules="rules.validateRequire">
            <el-select v-model="model.whetherdefault" filterable clearable>
              <el-option v-for="item in codeModel.whetherdefaultList" :key="item.code" :label="item.name" :value="parseInt(item.code)"/>
            </el-select>
          </el-form-item>
        </el-col>
			</el-row>
<!--			<el-row>-->
<!--				<el-col :span="24">-->
<!--					<el-form-item label="备注" prop="description" label-width="180px">-->
<!--						<el-input v-model="model.description" type="textarea" :rows="4"></el-input>-->
<!--					</el-form-item>-->
<!--				</el-col>-->
<!--			</el-row>-->
			<el-row>
				<el-col :span="12">
					<el-form-item label="排序码" prop="sortcode" label-width="180px">
						<el-input-number v-model="model.sortcode" controls-position="right" :step="1" :min="0" style="width:100%"></el-input-number>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="有效" prop="enabled">
						<el-radio-group v-model="model.enabled">
							<el-radio :label="parseInt(item.code)" v-for="item in codeModel.enabledList" :key="item.code">{{ item.name }}</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
			</el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :type="disableEditSubmit === false ? 'text' : 'primary'" @click="doCancelEdit">取消</el-button>
      <el-button type="primary" @click="doSubmitEdit" :loading="saveLoading" v-if="disableEditSubmit === false">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
/** mixins */
import { mixin_edit } from '@tapui/mixins/mixin_edit'
/** 子组件 */
/** 工具类 */
import { putAction, getAction, postAction, deleteAction } from '@tapui/utils/http'
/** VUE配置 */
export default {
  mixins: [mixin_edit],
  components: { },
  data() {
    return {
      moduleTitle: '岗位授权信息表',
      urlPrefix: '/mms/postAuthorizationInfo/',
      url: {
        // 额外的url
      },
      arrayvisibledeptcommon_code_range: [],
      noarrayvisible_common_code_range: [],
      arrayvisible_productline_range: [],
      noarrayvisible_productline_range: []
    }
  },
  computed: {
      productValidate() {
          return function(value) {
              if (this.model.visibledeptcommoncodetype && value === '1') {
                  return true
              } else if (this.model.novisiblecommoncodetype && value === '2') {
                  return true
              } else {
                  return false
              }
          }
      },
      sectionValidate() {
          return function(value) {
              if (this.model.visibledeptcommoncodetype && this.model.visibledeptcommoncodetype !== '1' && value === '1') {
                  return true
              } else if (this.model.visibleproductlinetype && this.model.visibleproductlinetype !== '1' && value === '2') {
                  return true
              } else if (this.model.novisiblecommoncodetype && this.model.novisiblecommoncodetype !== '1' && value === '3') {
                  return true
              } else if (this.model.novisibleproductlinetype && this.model.novisibleproductlinetype !== '1' && value === '4') {
                  return true
              } else {
                return false
              }
          }
      }
  },
  methods: {
      open() {
          this.model = {}
          this.arrayvisibledeptcommon_code_range = []
          this.noarrayvisible_common_code_range = []
          this.arrayvisible_productline_range = []
          this.noarrayvisible_productline_range = []
          this.$nextTick(() => {
              this.$refs.form.clearValidate()
              if (this.isAdd === true) {
                  this.title = '添加' + this.moduleTitle
                  if (this.isQueryInitData === true) {
                      // 后台进行数据初始化
                      this.queryInitData().then((_) => {
                          this.assignDefaultValue()
                          this.loaded()
                      })
                  } else {
                      // 前台进行数据初始化
                      this.$set(this.model, 'enabled', 1)
                      this.$set(this.model, 'sortcode', 0)
                      this.assignDefaultValue()
                      this.loaded()
                  }
              } else if (this.disableEditSubmit === false) {
                  this.title = '编辑' + this.moduleTitle
                  this.queryData(this.id).then((_) => {
                      this.loaded()
                  })
              } else {
                  this.title = '查看' + this.moduleTitle
                  this.queryData(this.id).then((_) => {
                      this.loaded()
                  })
              }
          })
      },
      loaded() {
          if (this.isAdd) {
              // this.$set(this.model, "visibledeptcommoncodetype", "1")
              // this.$set(this.model, "visibleproductlinetype", "1")
              // this.$set(this.model, "novisiblecommoncodetype", "1")
              // this.$set(this.model, "novisibleproductlinetype", "1")
          }
          // 页面加载完毕执行
          if (this.model.visibledeptcommoncoderange && this.model.visibledeptcommoncoderange !== '') {
              this.arrayvisibledeptcommon_code_range = this.model.visibledeptcommoncoderange.split(',')
          } else {
              this.arrayvisibledeptcommon_code_range = []
          }
          if (this.model.novisiblecommoncoderange && this.model.novisiblecommoncoderange !== '') {
              this.noarrayvisible_common_code_range = this.model.novisiblecommoncoderange.split(',')
          } else {
              this.noarrayvisible_common_code_range = []
          }
          if (this.model.visibleproductlinerange && this.model.visibleproductlinerange !== '') {
              this.arrayvisible_productline_range = this.model.visibleproductlinerange.split(',')
          } else {
              this.arrayvisible_productline_range = []
          }
          if (this.model.novisibleproductlinerange && this.model.novisibleproductlinerange !== '') {
              this.noarrayvisible_productline_range = this.model.novisibleproductlinerange.split(',')
          } else {
              this.noarrayvisible_productline_range = []
          }
      },
      beforeSubmit() {
        if (this.model.visibledeptcommoncodetype === '1') {
          this.model.visibledeptcommoncoderange = ''
        } else {
          this.model.visibledeptcommoncoderange = this.arrayvisibledeptcommon_code_range.join(',')
        }

        if (this.model.novisiblecommoncodetype === '1') {
          this.model.novisiblecommoncoderange = ''
        } else {
          this.model.novisiblecommoncoderange = this.noarrayvisible_common_code_range.join(',')
        }

        if (this.model.visibleproductlinetype === '1') {
          this.model.visibleproductlinerange = ''
        } else {
          this.model.visibleproductlinerange = this.arrayvisible_productline_range.join(',')
        }

        if (this.model.novisibleproductlinetype === '1') {
          this.model.novisibleproductlinerange = ''
        } else {
          this.model.novisibleproductlinerange = this.noarrayvisible_productline_range.join(',')
        }
          return true
      }
  }
}
</script>

